package cn.wolfcode.luowowo.member.service.Impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AsserUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }



    @Override
    public boolean checkPhone(String phone) {
        int c = userInfoMapper.checkPhone(phone);
        if (c > 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void sendVerifyCode(String phone) {
        //创建短信验证码
        String code = UUID.randomUUID().toString().replace("-", "")
                .substring(0, 4);
        StringBuilder sb = new StringBuilder(100);
        sb.append("你的短信验证码是").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME)
                .append("分钟内使用");

        System.out.println(sb);
        userInfoRedisService.setVerifyCode(phone,code);
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode)
    throws LogicException{
        AsserUtil.hasLength(phone,"手机号不能为空");
        AsserUtil.hasLength(nickname,"昵称不能为空");
        AsserUtil.hasLength(password,"密码不能为空");
        AsserUtil.hasLength(rpassword,"确认不能为空");
        AsserUtil.hasLength(verifyCode,"验证码不能为空");

        //判断密码是否一致
        AsserUtil.isEquals(password,rpassword,"两次密码不一致");
        //判断手机格式是否正确
        if(phone.length()!=11){
            throw  new LogicException("手机格式出错");
        }
        //判断手机号是否唯一
        if(!this.checkPhone(phone)){
            throw new LogicException("手机号已经被注册");

        }
        //判断短信验证码是否一致
        String code=   userInfoRedisService.getVerifyCode(phone);

        if(code==null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码失效或者错误");
        }
        //用户注册
        UserInfo user = new UserInfo();
        user.setHeadImgUrl("/images/default.jpg");
        user.setLevel(1);
        user.setNickname(nickname);
        user.setPassword(password);
        user.setPhone(phone);
        user.setState(UserInfo.STATE_NORMAL);
        userInfoMapper.insert(user);
    }

    @Override
    public String userLogin(String username, String password)throws LogicException {
    UserInfo user =    userInfoMapper.selectUserByUsernameAndPassword(username,password);
    if(user==null){
        throw  new LogicException("账号或密码错误");
    }
  String token= UUID.randomUUID().toString().replaceAll("-","");
    userInfoRedisService.setToken(token,user);




return token;

    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }


}